var home;
home = new Vue({

    el: '#home',
    //初始化数据类型
    data: {
        activeIndex: '1',
        centerDialogVisible:false,
        //商品对比数据
        contrastInformation:[],
        ids:'',
        //用户是否登录
        type:0,
        //商品类型
        hmtop: [],
        goodsType: [],
        //侧边导航栏信息
        leftNavigation: [],
        //商家信息缓存
        activiVo: [],
        //商家信息展示
        activity: [],
        mixture: [],
        //全部公告信息
        notice: [],
        //首页展示的10条公告信息
        noticeVo: [],
        //预警信息
        warn: [],
        cursor: {
            cursor: 'pointer',
        },
        //正文左侧logo
        sire: [],

        //判断搜索功能信息:1为商品，0为商家
        search: 1,
        isCollapse: true
    },
    //提交时间方法
    methods: {
        handleOpen:function(key, keyPath) {
            console.log(key, keyPath);
        },
        handleClose:function(key, keyPath) {
            console.log(key, keyPath);
        },

        //商品对比
        on_showClick: function (sire) {
            var _this = this;
            debugger
            var ID = sire.id;
            var T = true;
            var type = '';
            if (_this.contrastInformation != null) {
                for (var i = 0; i < _this.contrastInformation.length; i++) {
                    if (_this.contrastInformation[i].id == ID) {
                        _this.contrastInformation.splice(i, 1);
                        T = false;
                        sessionStorage.contrastInformation = JSON.stringify(_this.contrastInformation);
                    }
                    type = _this.contrastInformation[i].category;
                }
            }
            if (T) {
                if (_this.contrastInformation != null) {
                    if (_this.contrastInformation.length == 5) {
                        _this.$notify({
                            message: "请注意，最多选5个商品。",
                            type: 'error'
                        });
                        return;
                    } else {

                        if ((_this.contrastInformation.length > 0) && (type != sire.category)) {
                            _this.$notify({
                                message: "请注意，不同类型商品不允许比价。",
                                type: 'error'
                            });
                            return;
                        } else {
                            _this.contrastInformation.push(sire);
                            sessionStorage.contrastInformation = JSON.stringify(_this.contrastInformation);
                        }
                    }
                    $("#contrast").show();
                }else{
                    _this.contrastInformation.push(sire);
                    sessionStorage.contrastInformation = JSON.stringify(_this.contrastInformation);
                }
            }
        },
        //对比弹出框的取消事件
        on_hideClick: function (type) {
            debugger
            var _this = this;
            /*_this.contrastInformation = [];
            $(".close").click();*/
            $(".modal-backdrop.fade.in").remove();
            $(".on_show").removeAttr("checked");//取消
            $("#contrast").hide();
            sessionStorage.contrastInformation=_this.contrastInformation;
            window.location.reload();
        },
        //模态框
        on_shClick: function () {
            debugger
            var _this = this;
            _this.contrastInformation = [];
        },


        //查看公告信息
        entertip: function (noticeVo) {
            debugger
            $("#home").load("http://" + window.location.host + "/home/noticeInfo.html", function () {
                noticeInfo.noticeVo = noticeVo;
            });
        },

        //采购单位按钮数据清空
        addUnitEmpty:function(){
            var _this = this;
            _this.optionsUser.length;
            _this.unitEnter={};
            _this.optionsUser=[];
            if(this.optionsUser.length == 0){
                $.post("/sys/user/seleceOfficeUser", "", function (data) {
                    for (var i = 0; i < data.data.sysUser.length; i++) {
                        debugger
                        var map1 = {};
                        map1.value = data.data.sysUser[i].id;
                        map1.label = data.data.sysUser[i].name;
                        _this.optionsUser.push(map1);
                    }
                });
            }



        },
        //左侧导航栏
        mouseover:function(id){
            $("#"+id).css("display","block");
        },
        onAddDataBefore:function (mixVal) {
            debugger
            var _this=this;
                for (var l = 0; l < _this.contrastInformation.length; l++) {
                        if(l==0){
                            _this.ids=_this.contrastInformation[l].id;
                        }else{
                            _this.ids=_this.ids+ ","+ _this.contrastInformation[l].id;
                        }
                }
                _this.onAddData(mixVal);
                _this.on_hideClick();
                _this.on_shClick();

        },
        //添加购物车
        onAddData: function (mixVal) {
            var _this = this;
            if(_this.type==0){
                _this.$message({
                    message: '警告哦，您未登录！',
                    type: 'warning'
                });
            }else{
            for (var i = 0; i < _this.activiVo.length; i++) {
                if (_this.activiVo[i].id == mixVal.businessesId) {
                    if (_this.activiVo[i].type == 0) {
                        _this.$message({
                            message: '警告哦，该商家未入围不能添加其商品至购物车！',
                            type: 'warning'
                        });
                    } else {
                        if (mixVal.install == 0||mixVal.install ==2) {
                            _this.$message({
                                message: '警告哦，该商品已下架不能添加至购物车！',
                                type: 'warning'
                            });
                        } else {
                            debugger
                            $.post("/home/order_detail/insertShopCart", {"lim": JSON.stringify(mixVal),"sell": _this.activiVo[i].type,"ids":_this.ids}, function (data) {
                                if (data.data.shopCart == 1) {
                                    _this.$message({
                                        message: '恭喜你，该商品已添加至购物车',
                                        type: 'success'
                                    });
                                } else {
                                    _this.$message({
                                        message: '警告哦，商品未添加至购物车',
                                        type: 'warning'
                                    });
                                }
                            });
                        }
                    }
                }

            }
            }

        },
        //重定向到商品详情页面
        onTouch: function (mixVal) {
            var _this = this;
            $("#home").load("http://" + window.location.host + "/home/introduction.html", function () {
                introduction.product = mixVal;
                for (var i = 0; i < _this.activiVo.length; i++) {
                    if (mixVal.businessesId == _this.activiVo[i].id) {
                    }
                }

            });
        },
        //搜索功能(商品和商家的判断)
        onShop: function (i) {
            var _this = this;
            _this.search = i;
        },
        //商家信息重定向到商家详情页面
        onActivity: function (activityList) {
            debugger
            $.post("/home/commodity/selectCommodityByMerchant",{"id":activityList.id},function(data){
                $("#home").load("http://" + window.location.host + "/person/selldetail.html", function () {
                    activityList.creationTime = moment().format("YYYY-MM-DD")
                    selldetail.sellinformation = activityList;
                    selldetail.product = data.data.commodityList;
                });
            });
        },
        //搜索按钮单击事件
        shopSelect: function () {
            var _this = this;
            $.post("/home/commodity/shopSelect", {
                "Name": $("#searchInput").val(),
                "type": _this.search
            }, function (data) {
                //跳转商品
                if (_this.search == 1) {
                    $("#home").load("http://" + window.location.host + "/home/search.html", function () {
                        debugger
                        search.limit = data.data.commodity;
                    });
                    //跳转商家信息
                } else {
                    $("#home").load("http://" + window.location.host + "/person/sellinformation.html", function () {
                        sellinformation.list = data.data.commodity;
                    });
                }
            });

        },

        //预警信息
        show_warn: function () {
            var _this = this;

            $.post("/home/order_detail/selectStatistics", "", function (data) {
                debugger;
                _this.warn = data.data.warnText;
            });
        },
        //菜单模糊查询
        hrefLike:function(hrefLike){
           $.post("/home/menu/likeSelect",{"Menu":JSON.stringify(hrefLike)},function(data){
                $("#home").load("http://" + window.location.host + "/home/search.html", function () {
                    search.limit = data.data.commodityList;
                });
            });
        },
    },
    //页面加载后初始化方法
    created: function () {
        var _this = this;
        //查看用户是否已经登录
        $.post("/home/dataCache/selectDataCache", "", function (data) {
            if (data.code == 200) {
                _this.type = 1
            }

        });
        //获得菜单标签信息
        $.post("/home/menu/select", "", function (data) {
            _this.leftNavigation = data.data.Menu;
            _this.mixture = data.data.Comm;
        });
        //获得公告信息
        $.post("/home/notice/select", "", function (data) {
            _this.noticeVo = data.data.noticelist;

        });
        //获得商家信息
        $.post("/home/merchant/selectAll", "", function (data) {
            _this.activiVo = data.data.merchant;
            _this.activity = _this.activiVo.slice(0, 4);
        });
        //初始化横向菜单列表
        var goodsTypeList = [
            {name: "询价表", href: "/home/offers.html"},
            /*{name: "报价单", href: "../home/quotation.html"},*/
        ];
        _this.goodsType = goodsTypeList;
        var sessiones= sessionStorage.contrastInformation;
        if(sessiones==""||sessiones==null){

        }else{
            _this.contrastInformation = JSON.parse(sessiones);
            if (_this.contrastInformation !=null && _this.contrastInformation !='') {
                if (_this.contrastInformation.length > 0) {
                    $("#contrast").show();
                }
            }
        }


    }
});

/*设置搜索框文字背景样式*/
$(function () {
    $(".search-tab a").on("click", function () {
        $(".search-tab a").css("background-color", "");
        $(".search-tab a").css("color", "#333");
        $(this).css("background-color", "#F03726");
        $(this).css("color", "#fff");
    })
});